<?php

class News_Model extends Model {

    function News_Model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_entries()
    {
		$this->load->database();
		$query = $this->db->query('SELECT * FROM gcms_news ORDER BY news_date DESC');
		echo '<ul style="list-style-image:url('.base_url().'system/images/page.png)">';
		foreach ($query->result() as $row)
		{
			echo '<li>';
			echo '<a href="news/edit/'.$row->ID.'">'.$row->news_heading.'</a> (<a href="#" onclick="deleteNews(\''.$row->ID.'\');">Delete</a>) <span class="meta">'.mysql_to_date($row->news_date).'</span>';
			echo '</li>';
		}
		echo '</ul>';
    }

    function insert_entry()
    {
		$this->load->database();
		$this->load->helper('url');
		
        $heading = $_POST['heading'];
		$content = $_POST['content'];
		$date = date('Y-m-d H:i:s');
		
		$data = array(
               'news_date' => $date,
			   'news_content' => $content,
			   'news_heading' => $heading
            );

		$this->db->insert('gcms_news', $data); 
    }

    function update_entry()
    {
		$this->load->database();
		
        $heading = $_POST['heading'];
		$content = $_POST['content'];
		
		$data = array(
			   'news_content' => $content,
			   'news_heading' => $heading
            );

		$this->db->where('ID', $this->uri->segment(3));
		$this->db->update('gcms_news', $data); 
    }
	
	function delete_entry($id)
	{
		$this->load->database();
		$this->db->where('ID', $id);
		$this->db->delete('gcms_news'); 
	}

}

?>